package com.ebupt.migu.music.autoTri.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.music.autoTri.Entity.MarketingBean;
import com.ebupt.migu.music.autoTri.Entity.ResultBean;
import com.ebupt.migu.music.autoTri.Entity.SendResult;
import com.ebupt.migu.music.common.exception.MiguRuntimeException;
import com.ebupt.migu.music.common.util.HttpUtils;
import com.ebupt.migu.music.common.util.HttpsUtils;
import com.ebupt.migu.music.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**  门户查询统一CMS mv的接口
 * @author xuhongsong
 * @version 1.0
 * @date 2020/7/28 10:17
 */
@Slf4j
public class MvSender implements Sender{
    @Override
    public List<ResultBean> Send(Map<String, Object> paramsMap) {
        //  第一次执行的
        SendResult sendResult=mvResult(paramsMap);
        List<ResultBean>  list=sendResult.getResultBeanList();
        Boolean IsHave=false;
        switch (sendResult.getIsEnd()){
            case "true":
                IsHave=true;
                break;
            case "false":
                IsHave=false;
                break;
        }
        int code=1;
        while (IsHave){
            paramsMap.put("startQueryId",sendResult.getStartQueryId());
            sendResult=mvResult(paramsMap);
            code++;
            list.addAll(sendResult.getResultBeanList());
        }
        log.info("统一CMS MV数据次数:{}"+code);
        return  list;
    }


    // 门户查询统一CMS歌曲的接口
    private SendResult mvResult(Map<String,Object>  paramsMap){
        log.info("调用门户查询统一CMS MV的接口, catalogRequest:{}", paramsMap.toString());
        String result;
        try {
            result = HttpUtils.doGetRequest(paramsMap.get("url").toString(), paramsMap);
        } catch (Exception e) {
            log.error("调用CMS MV接口失败", e);
            throw new MiguRuntimeException("调用CMS MV接口失败", e);
        }
        log.info("CMS MV实体返回:{}", result);
        //  返回map
        Map songMap = JsonUtils.json2Obj(result, Map.class);
        if (!"000000".equals(songMap.get("state"))) {
            log.error("获取CMS MV数据失败");
            throw new MiguRuntimeException("获取CMS MV数据失败" + songMap.get("message"));
        }
        Map contentMap = (Map) songMap.get("content");
        // 所有更新数据是否已经完全查询  true or false
        String isEnd=contentMap.get("isEnd").toString();
        // 最新的时间戳Id
        String lastChangeId=contentMap.get("lastChangeId").toString();
        JSONArray contentList = (JSONArray) contentMap.get("list");
        if (contentList == null || contentList.isEmpty()) {
            log.error("获取CMS MV实体接口数据为空");
        }
        List<Map> ObjectList = JSONObject.parseArray(contentList.toJSONString(),Map.class);
        //  构造结果集
        List<ResultBean> list= ObjectList.stream().map(
                e->new ResultBean(Integer.valueOf(e.get("id").toString()),
                        e.get("mvMaterialName").toString(), e.get("mvMaterialCreateTime").toString(),
                        e.get("mvMaterialUpdateTime").toString(),1,e.get("mvMaterialSingers").toString())
        ).collect(Collectors.toList());
        return   new SendResult(isEnd,lastChangeId,list);
    }
}
